CC  = gcc
F90 = gfortran
PYTHON = python
# F90FLAG = -fPIC -shared -openmp
OPENMP = -fopenmp
LIB = "D:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\lib.exe"

NAME = eqs

ifeq ($(OS),Windows_NT)
	DLL_PATH = E:/Linux/usr/bin
	LIB_PATH = D:/Documents/Qt/EQSignal/EQSignal/lib
	BLASLAPACK = $(DLL_PATH)/libblas.dll $(DLL_PATH)/liblapack.dll
	# BLASLAPACK=libopenblas.dll
	FFTW = $(DLL_PATH)/libfftw3-3.dll
	OBJ_EXT=obj
	DLL_EXT=dll
	LIB_EXT=lib
	DEP_LIB=$(NAME).$(LIB_EXT)
	F90FLAG = -shared -Ofast $(OPENMP)
	# LFLAG = -Wl,--output-def,lib$(NAME).def
	PREFIX = E:/Linux/usr/bin
else
	BLASLAPACK = -lblas -llapack
	FFTW = -lfftw3
	OBJ_EXT =o
	LIB_EXT=a
	DEP_LIB =
	PREFIX = /usr/lib
	ifeq ($(shell uname),Darwin)
		DLL_EXT=dylib
		F90FLAG = -fPIC -shared -Ofast $(OPENMP)
	else
		DLL_EXT=so
		F90FLAG = -fPIC -shared -Ofast $(OPENMP)
	endif
endif

DYLIB = lib$(NAME).$(DLL_EXT)
STLIB = lib$(NAME).$(LIB_EXT)

SOURCE = basic.f90 poly.f90 avd.f90 response.f90 spectra.f90 utils.f90
OBJ = basic.o poly.o avd.o response.o spectra.o utils.o

all: $(DYLIB)

lib: $(DYLIB) $(DEP_LIB)

dylib: $(DYLIB)

stlib: $(STLIB)

$(DYLIB): $(OBJ)
	$(F90) $(OBJ) $(F90FLAG) $(LFLAG) $(BLASLAPACK) $(FFTW) -o $(DYLIB)

$(STLIB): $(OBJ)
	$(AR) rcs $(STLIB) $(OBJ)

%.o:%.f90
	$(F90) -c $< $(F90FLAG) -o $@

%.lib:lib%.dll
	$(PYTHON) dll2lib.py $<

install: $(DYLIB) $(DEP_LIB)
	cp $(DYLIB) $(PREFIX)
	-cp $(DEP_LIB) $(LIB_PATH)

uninstall:
	rm -vf $(PREFIX)/$(NAME).$(DLL_EXT)

.PHONY : clean, clear, test

clean:
	rm -vf $(DYLIB) $(STLIB) *.o *.mod $(DEP_LIB)

clear:
	rm -vf *.o *.mod
